﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;

namespace IntoTheVoid
{
	public partial class AdvertPicControl : System.Windows.Controls.UserControl
	{
		public AdvertPicControl()
		{
			InitializeComponent();
		}

		private List<BitmapImage> currentListImg = null;
		public List<BitmapImage> CurrentListimg
		{
			set
			{
				initLbtCtrl(value);
			}
		}

		private List<RadioButton> currentListRdtBtn = null;

		private Timer timer = null;
		public void initLbtCtrl(List<BitmapImage> imgList)
		{
			if (imgList == null) { return; }
			bool isChecked = true;
			int rdIndex = 0;
			imgList.ForEach((item) => {
				RadioButton rdBtn = new RadioButton();
				if (isChecked) { rdBtn.IsChecked = true; isChecked = false; }
				rdBtn.Tag = rdIndex;
				rdIndex++;
				rdBtn.Click += RdBtn_Click;
				stack.Children.Add(rdBtn);
				if (currentListImg == null) { currentListImg = new List<BitmapImage>(); }
				if (currentListRdtBtn == null) { currentListRdtBtn = new List<RadioButton>(); }
				currentListImg.Add(item);
				currentListRdtBtn.Add(rdBtn);
			});
			this.displayImg.Source = currentListImg[0];
			if (timer == null)
			{
				timer = new Timer(new TimerCallback((p) =>
				{
					this.Dispatcher.Invoke(new Action(() =>
					{
						next();
					}));
				})
					, null, 3000, 3000);
			}
		}

		private void PreImg_MouseDown(object sender, RoutedEventArgs e)
		{
			before();
		}
		void before()
		{
			int count = stack.Children.Count;
			bool IsFirstEnter = true;
			foreach (RadioButton item in stack.Children)
			{
				if (item.IsChecked == true && IsFirstEnter)
				{
					IsFirstEnter = false;
					item.IsChecked = false;
					int index = (int)item.Tag;
					if (index == 0)
					{
						index = count - 1;
					}
					else
					{
						index--;
					}
					this.displayImg.Source = currentListImg[index];
					currentListRdtBtn[index].IsChecked = true;
				}
			}
		}
		private void NextImg_MouseDown(object sender, RoutedEventArgs e)
		{
			next();
		}
		void next()
		{
			int count = stack.Children.Count;
			bool IsFirstEnter = true;
			foreach (RadioButton item in stack.Children)
			{
				if (item.IsChecked == true && IsFirstEnter)
				{
					IsFirstEnter = false;
					item.IsChecked = false;
					int index = (int)item.Tag;
					if (index == count - 1)
					{
						index = 0;
					}
					else
					{
						index++;
					}
					this.displayImg.Source = currentListImg[index];
					currentListRdtBtn[index].IsChecked = true;
				}
			}
		}
		private void RdBtn_Click(object sender, RoutedEventArgs e)
		{
			foreach (RadioButton item in stack.Children)
			{
				item.IsChecked = false;
			}
			RadioButton rdBtn = (RadioButton)sender;
			rdBtn.IsChecked = true;
			this.displayImg.Source = currentListImg[(int)rdBtn.Tag];

		}

		private void Button_Click(object sender, RoutedEventArgs e)
		{
			next();
		}

		private void displayImg_MouseEnter(object sender, MouseEventArgs e)
		{
			Left.Visibility = Visibility.Visible;
			Right.Visibility = Visibility.Visible;
		}

		private void displayImg_MouseLeave(object sender, MouseEventArgs e)
		{
			Left.Visibility = Visibility.Hidden;
			Right.Visibility = Visibility.Hidden;
		}

		private void Right_MouseEnter(object sender, MouseEventArgs e)
		{
			Right.Visibility = Visibility.Visible;

		}

		private void Left_MouseEnter(object sender, MouseEventArgs e)
		{
			Left.Visibility = Visibility.Visible;

		}
	}
}

